const app = getApp();
import { Cars } from "../../../models/cars";
import { popup } from "../../../utils/util";
const cars = new Cars();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalImgUrl.config.baseImgUrl,
    carList: [],
    scrollH: "auto",
    pageNum: 1,
  },
  handlerGobackClick() {
    wx.navigateBack();
  },
  toaddCar() {
    wx.navigateTo({
      url: "../addCars/index",
    });
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../carDetail/index?carId=" + id,
    });
  },
  deleteCar(e) {
    let carId = e.currentTarget.dataset.id;
    popup(() => {
      cars.detele(carId).then(() => {
        this.setData({
          pageNum: 1,
          carList: [],
        });
        this.getList();
      });
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum: 1,
      carList: [],
    });
    this.getList();
  },
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        console.log(res);
        let height = res.height;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },
  getList() {
    cars.list(this.data.pageNum, 10).then((res) => {
      let carList = this.data.carList.concat(res.result.records || []);
      this.setData({
        carList,
      });
    });
  },
  scrollBottom() {
    this.setData({
      pageNum: this.data.pageNum + 1,
    });
    this.getList();
  },
});
